package view{
	import events.GameEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import utils.SwfElements;

	public class UpgradeBar extends Sprite{
		
		/**
		 * 容器 
		 */		
		public var box:Sprite;
		/**
		 * 声明SWF 文件对象  
		 */		
		private var swfElement:SwfElements;
		
		/**
		 * 升级选择器的单历 
		 */		
		private static var instence:UpgradeBar;
		
		
		/**
		 * 用于设置升级选择器的visiable属性; 
		 */		
		public var isShow:Boolean = false;
		
		/**
		 *  升级选择器外观;
		 */		
		private var upgradeMC:MovieClip;
		
		/**
		 * 出售按钮 
		 */		
		private var sellBtn:MovieClip;
		
		/**
		 * 升级按钮 
		 */	
		private var upgradeBtn:MovieClip;
		
		/**
		 * 弹出框 
		 * 
		 */
		private var promptBox:PromptBox;
		
		public function UpgradeBar(){
			box = new Sprite();
			addChild(box);
			swfElement = new SwfElements();
			box.addChild(swfElement);
			addEventListener(GameEvent.SWF_LOADER_COMPLETE,swfCompleteHandle);
			
			promptBox = PromptBox.getInstence();
			
		}
		
		
		/**
		 * 单历 
		 * @return 
		 * 
		 */		
		public static function getInstence():UpgradeBar{
			if(!instence) instence = new UpgradeBar();
			return instence;
		}
		
		/**
		 * 当swf加载完成时获取其中影片剪辑的值 
		 * @param event
		 * 
		 */		
		protected function swfCompleteHandle(event:GameEvent):void{
			upgradeMC = swfElement.getMC("UpgradeMC");
			box.addChild(upgradeMC);
			addBtns();
			
		}
		
		
		/**
		 * 添加塔的选择按钮 
		 * 
		 */	
		private function addBtns():void{
			upgradeBtn = swfElement.getMC("UpgradeBtn");
			upgradeBtn.name = "UpgradeBtn";
			sellBtn = swfElement.getMC("SellBtn");
			sellBtn.name = "SellBtn";
			
			box.addChild(upgradeBtn);
			box.addChild(sellBtn);
			
			upgradeBtn.x = upgradeMC.x;
			upgradeBtn.y = upgradeMC.y - 47;
			
			sellBtn.x = upgradeMC.x;
			sellBtn.y = upgradeMC.y + 55;
			
			
			upgradeBtn.buttonMode = true;
			sellBtn.buttonMode = true;
			
			upgradeBtn.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandle);
			sellBtn.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandle);
			
			upgradeBtn.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
			sellBtn.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
			
			upgradeBtn.addEventListener(MouseEvent.CLICK,clickHandle);
			sellBtn.addEventListener(MouseEvent.CLICK,clickHandle);
			
			
		}	
		
		private function clickHandle(event:MouseEvent):void{
			var mc:MovieClip = event.target as MovieClip;
			//将当前build的位置,所选择的塔的类型,包装成一个对象发送出去
			var obj:Object={target:mc,px:x,py:y};
			var evt:GameEvent = new GameEvent(GameEvent.CLICK_BUILD_TYPE,obj);
			dispatchEvent(evt);
		}
		
		private function mouseOverHandle(event:MouseEvent):void{
			var mc:MovieClip = event.target as MovieClip;
			box.addChild(promptBox);
			setChildIndex(box,numChildren-1);
			/*promptBox.x = mc.x + 18;
			promptBox.y = mc.y - 115;
			
			promptBox.textTitle = "一级炮塔";
			promptBox.textDescribe = "二级炮塔";*/
//			trace(promptBox.setPosition(this));
			setPosition(mc);
			promptBox.orShow();
			
			
		}
		
		
		
		private function mouseOutHandle(event:MouseEvent):void{
			promptBox.orShow();
		}
		
		/**
		 * 建筑物选择器在显示和隐藏两种状态之间切换; 
		 * 
		 */		
		public function orShow():void{
			isShow = ! isShow;
			visible = isShow;
		}
		
		
		public function setPosition(mc:MovieClip):void{
//			trace("1",x,promptBox.x, promptBox.width);
			if((x + promptBox.width)> stage.stageWidth ){
				promptBox.x = mc.x - promptBox.width - 17;
				
			}else{
				promptBox.x = mc.x + 17;
			}
			
			if( (y + height) > stage.stageHeight ){
				promptBox.y = mc.y - promptBox.height + 15;
			}else{
				promptBox.y = mc.y -  20;
			}
			
		}
		
		
		
		
		
		
	}
}